//
//  XZTestDynamicDetailVC.m
//  Teecloud
//
//  Created by PC on 2018/12/25.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZTestDynamicDetailVC.h"
//View
#import "XZTestHeadView.h"
#import "XZTestDetailFirstCell.h"
#import "XZTestDetailSecondCell.h"
//Model
#import "XZTestModel.h"
#import "XZPostInfos.h"
#import "XZTimeRoomInfos.h"
@interface XZTestDynamicDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) XZTestHeadView *headView;
@property (nonatomic, strong) XZTestModel *model;
@end

@implementation XZTestDynamicDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self getTestMonitorInfo];
}
#pragma mark - Networking
- (void)getTestMonitorInfo{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:self.testId forKey:@"testId"];
    kWeakObject(self)
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=GetTestMonitorInfo&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [weakself.tableView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSDictionary *dic = [responseObject objectForKey:RESULT_NAME_Data];
                XZTestModel *model = [XZTestModel mj_objectWithKeyValues:dic];
                weakself.title = model.SummaryInfo.Title;
                weakself.model = model;
                //有值才设置头视图
                if (model.SummaryInfo != nil) {
                    weakself.tableView.tableHeaderView = weakself.headView;
                    weakself.headView.model = model.SummaryInfo;
                }else{
                   weakself.tableView.tableHeaderView = [UIView new];
                }
                //三者同时没值就设置无数据
                if (model.TimeRoomInfos.count == 0  && model.PostInfos.count == 0 && model.SummaryInfo == nil) {
                    [weakself.tableView setupEmptyDataText:@"暂无数据" secoundText:@"" buttonTit:@"" verticalOffset:0 emptyImage:NODATAIMG tapBlock:^{
                        
                    } butClick:^{
                        
                    }];
                }
                [weakself.tableView reloadData];
            }else{
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}
#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.model.TimeRoomInfos.count;
    }else{
       return self.model.PostInfos.count;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        XZTestDetailFirstCell *firstCell = [tableView dequeueReusableCellWithIdentifier:@"XZTestDetailFirstCell"];
        XZTimeRoomInfos *model = self.model.TimeRoomInfos[indexPath.row];
        firstCell.model = model;
        return firstCell;
    }else{
        XZTestDetailSecondCell *secondCell = [tableView dequeueReusableCellWithIdentifier:@"XZTestDetailSecondCell"];
        XZPostInfos *model = self.model.PostInfos[indexPath.row];
        secondCell.model = model;
        return secondCell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 110.f;
    }else{
        return 35.f;
    }
}
#pragma mark - Setter
- (XZTestHeadView *)headView{
    if (!_headView) {
        _headView = [XZTestHeadView viewFromXib];
        _headView.frame = CGRectMake(0, 0, kScreenWidth, 70);
    }
    return _headView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kBottomSafeHeight-kNavBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.delegate =self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.showsVerticalScrollIndicator = false;
        _tableView.showsHorizontalScrollIndicator  = false;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"XZTestDetailFirstCell" bundle:nil] forCellReuseIdentifier:@"XZTestDetailFirstCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"XZTestDetailSecondCell" bundle:nil] forCellReuseIdentifier:@"XZTestDetailSecondCell"];
        //上下拉刷新
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getTestMonitorInfo)];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)makeTimer{
    
}

@end
